package com.sofwin.service.impl;

import com.sofwin.dto.ResourceDto;
import com.sofwin.mapper.SysResourceMapper;
import com.sofwin.pojo.SysResource;
import com.sofwin.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private SysResourceMapper mapper;

    @Override
    public List<ResourceDto> selectAllResource() {
        return mapper.selectAllResourceForTree();
    }
    /**
     * @Description: 通过RoleId获取角色的权限
     * @Author: Mr.jhx
     * @Date: 2021/4/17 16:44
     * @Param:
     * @Return: * @return: java.util.List<com.sofwin.pojo.SysResource>
     **/
    @Override
    public List<SysResource> selectAllResource2() {
        return mapper.selectAllResourceForTree2();

    }


    /**
     * @Description: 添加或修改权限信息
     * @Author: Mr.jhx
     * @Date: 2021/4/17 16:50
     * @Param: * @param resource:
     * @Return: * @return: boolean
     **/
    @Override
    public boolean save(SysResource resource) {
        if (resource.getId()!=null){
            return mapper.updateByPrimaryKeySelective(resource)==1?true:false;
        }
        return mapper.insertSelective(resource)==1?true:false;
    }

    /**
     * @Description: 通过id获取权限
     * @Author: Mr.jhx
     * @Date: 2021/4/17 16:59
     * @Param: * @param id:
     * @Return: * @return: com.sofwin.pojo.SysResource
     **/
    @Override
    public SysResource selectById(Integer id) {
        return mapper.selectById(id);
    }

    /**
     * @Description: 通过参数id删除权限
     * @Author: Mr.jhx
     * @Date: 2021/4/17 17:00
     * @Param: * @param id:
     * @Return: * @return: boolean
     **/
    @Override
    public boolean deleteById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1;
    }

    @Override
    public List<ResourceDto> selectResourceByRoled(Integer roleId) {
        return mapper.selectAllResourceForTreeByRoleId(roleId);
    }

    @Override
    public List<ResourceDto> selectRoleResource(Integer roleId) {

        return mapper.roleResource(roleId);
    }
    /**
     * @Description: 通过RoleId获取角色的权限
     * @Author: Mr.jhx
     * @Date: 2021/4/17 16:41
     * @Param: * @param roleId:
     * @Return: * @return: java.util.List<com.sofwin.pojo.SysResource>
     **/
    @Override
    public List<SysResource> selectResourceByRoleId(Integer roleId) {
        return mapper.selectResourceByRoleId(roleId);
    }

    /**
    * @Description:  查询所有权限信息
    * @Param:
    * @return: * @return: java.util.List<com.sofwin.pojo.SysResource>
    * @Author: Mr.Xq
    * @Date: 2021/4/23 0023
    */
    @Override
    public List<SysResource> selectAllResources() {
        return mapper.selectByExample(null);
    }

}
